package com.magina.project.controller;

import com.magina.project.entity.SysLog;
import com.magina.project.repository.JpaDemoEntityRepo;
import com.magina.project.repository.SysLogMapper;
import com.magina.project.repository.SysLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @ClassnName :com.magina.project.controller.SysLogController
 * @Author： Magina.Hu
 * @Date： 2019/5/14
 * @Description：
 * @Version: V1.0
 * Copyright (c) 2019-2020 .All rights reserved.
 */
@RestController
@RequestMapping("/SysLog")
public class SysLogController {

    @Autowired
    private SysLogRepository sysLogRepository;

    @Autowired
    private SysLogMapper sysLogMapper;


    @GetMapping("/findAll")
    private Object findAll(){
        // 分页查询第一页用户，限制每页10条记录，按创建时间降序排序
        int page = 0, size = 10;
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by(Sort.Order.desc("created")));
        Page<SysLog> userPage = sysLogRepository.findAll(pageRequest);
        return userPage;
    }

    //http://localhost:8092/jpa/SysLog/find1?id=2cd50685a77a49f839ae445b48e073c5&moduleName=Exception&operationLogType=
    @GetMapping("/findByMybatisAndJpa")
    private Object findByMybatisAndJpa(@RequestParam String id,@RequestParam String moduleName,@RequestParam String operationLogType){
       // moduleName = null;
        //测试 jpa动态空值入参查询
       List list =  sysLogRepository.findByModuleNameAndOperationLogType(moduleName,operationLogType);
       List list1 =  sysLogRepository.findByModuleNameAndOperationLogType1(moduleName,operationLogType);
       //测试mybatis查询
       SysLog s =sysLogMapper.selectByPrimaryKey(id);
       return list;
    }

    @GetMapping("/add")
    private Object add(@RequestParam String id){
        SysLog record = new SysLog();
        record.setIpAddress("as");
        record.setOperatorAccount("admin");
        record.setOperatorName("admin");
        record.setOperatorDate(LocalDateTime.now());
        record.setOperatorOrgCode("");
        record.setOperatorOrgName("");
        record.setOperatorSystem("asd");
        record.setOperatorBrowser("asd");
        record.setClassName("asd");
        record.setMethodName("asd");
        record.setArguments("asd");
        record.setModuleName("asd");
        record.setDescription("asdasd");
        record.setOperationLogType("1");
        record.setOperationStatus("1");
        record.setCreatorId("asd");
        record.setCreator("asd");
        record.setModifierId("asd");
        record.setModifier("asd");
        sysLogRepository.save(record);//测试主键生成策略
        SysLog sysLog = sysLogRepository.getOne(id);
        sysLog.setModuleName(UUID.randomUUID().toString());
        sysLogRepository.save(sysLog);//测试save底层支持update/insert
        return record.getId();
    }

    //http://localhost:8092/jpa/SysLog/deleteByJpaAnno?id=asd
    @GetMapping("/deleteByJpaAnno")
    public void deleteByJpaAnno(String id ){
        //测试逻辑删除
        sysLogRepository.deleteById(id);
    }


}
